'use strict';
/**
 * @fileoverview VIP
 * @author sundbcn@gmail.com (sun debing)
 */

var mongoose = require('mongoose');
var moment = require('moment');
var _ = require('lodash');
var constantUtil = require('../util/constantUtil');
var vipMgr = require('../mgr/vipMgr');
var dataApi = require('../libs/dataApi');

/**
 * VIP功能
 */
var vipSchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 奖励领取的vip等级列表
    received_vip_level_list: [Number],

    // 已经购买礼包列表
    have_buy_package_list: [Number],

    // 上次刷新时间
    last_refresh_time: {
        type: Date,
        default: 0
    },

    // vip特权商城已购买列表
    vip_privilege_buy_list: [Number],
});

vipSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 加入ID到vip特权商城已购买列表
 * @return {[type]} [description]
 */
vipSchema.methods.getVipPrivilegeBuyList = function (shop_id) {
    if (_.indexOf(this.vip_privilege_buy_list, shop_id) == -1) {
        this.vip_privilege_buy_list.push(shop_id);
        return true;
    }
    return false;
};

vipSchema.methods.isVipPrivilegeBuyList = function (shop_id) {
    return _.indexOf(this.vip_privilege_buy_list, shop_id) == -1;
};

/**
 * 添加已经领取vip等级
 * @return {[type]} [description]
 */
vipSchema.methods.addReceiveVipLevel = function (vip_level) {
    if (!this.isCanReceive(vip_level)) {
        return;
    }
    this.received_vip_level_list.push(vip_level);
};

/**
 * 是否可领取vip奖励
 * @return {[type]} [description]
 */
vipSchema.methods.isCanReceive = function (reward_vip, vip) {
    if (reward_vip > vip) {
        return false;
    }

    return _.indexOf(this.received_vip_level_list, reward_vip) == -1;
};

/**
 * 刷新
 * @return {[type]} [description]
 */
vipSchema.methods.refresh = function () {
    this.last_refresh_time = Date.now();
    this.have_buy_package_list = [];
};

/**
 * 是否可以购买特权礼包
 * @return {[type]} [description]
 */
vipSchema.methods.isCanBuy = function (property_type) {
    return _.indexOf(this.have_buy_package_list, property_type) == -1;
};

/**
 * 添加礼包
 * @return {[type]} [description]
 */
vipSchema.methods.addVipPackage = function (property_type) {
    this.have_buy_package_list.push(property_type);
};

/**
 * 获取上次刷新时间
 * @return {[type]} [description]
 */
vipSchema.methods.getLastRefreshTime = function () {
    return this.last_refresh_time;
};

/**
 * 获取修改
 * @return {[type]} [description]
 */
vipSchema.methods.getLastChange = function () {
    return this.toJSON();
};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
vipSchema.methods.push = function () {
    this.pushModify();
};

vipSchema.methods.pushModify = function () {
    vipMgr.sendVipChanged(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send vip message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

if (!vipSchema.options.toJSON) {
    vipSchema.options.toJSON = {};
}
/* jshint unused:false */
vipSchema.options.toJSON.transform = function (doc, ret) {
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Vip', vipSchema);
